#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <el.h>
#include <bolt.h>
#include "scps.h"
#include "scpstp.h"
#include "tp_sched.h"
#include "tp.h"
#include "tp_debug.h"
#include "rs_config.h"

BOLT(gw_set_c_netstat_int, "gateway set c netstat-interval <interval>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid interval\n");
    return;
  }
  get_ifs()->c_netstat_interval = v;
}

BOLT(gw_set_c_localudp_port, "gateway set c local-udp-port <port>")
{
  uint16_t v;
  const char *argz = pop_argz();
  if (!str2u16(argz,&v)) {
    printf("invalid port\n");
    return;
  }
  get_ifs()->c_scps_local_udp_port = v;
  ll_set_udp_port(v);
}
        
BOLT(gw_set_c_remoteudp_port, "gateway set c remote-udp-port <port>")
{
  uint16_t v;
  const char *argz = pop_argz();
  if (!str2u16(argz,&v)) {
    printf("invalid port\n");
    return;
  }
  get_ifs()->c_scps_remote_udp_port = v;
  ll_set_udp_port1(v);
}

BOLT(gw_set_c_div_start_rule, "gateway set c div-start-rule <rule>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid rule\n");
    return;
  }
  get_ifs()->c_divert_start_rule = v;
}

BOLT(gw_set_c_div_insert_rule, "gateway set c div-insert-rule <rule>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid rule\n");
    return;
  }
  get_ifs()->c_divert_insert_rule = v;
}

BOLT(gw_set_c_other_xrate_drop, "gateway set c other-xrate-drop <xrate-drop>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid xrate drop\n");
    return;
  }
  get_ifs()->c_other_proto_xrate_drop = v;
}

BOLT(gw_set_c_other_nonip, "gateway set c other-nonip <other-nonip>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid nonip\n");
    return;
  }
  get_ifs()->c_other_proto_non_ip = v;
}

BOLT(gw_set_c_tap_remote_access, "gateway set c tap-remote-access <tap-remote-access>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid tap remote access\n");
    return;
  }
  get_ifs()->c_tap_remote_access = v;
}

BOLT(gw_set_c_other_qlen, "gateway set c other-qlen <qlen>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid qlen\n");
    return;
  }
  get_ifs()->c_other_proto_qlen = v;
}

BOLT(gw_set_c_clust_thresh, "gateway set c clust-thresh <thresh>")
{
  uint32_t v;
  const char *argz = pop_argz();
  if (!str2u32(argz,&v)) {
    printf("invalid thresh\n");
    return;
  }
  get_ifs()->c_clust_thresh = v;
}

BOLT(gw_set_c_divport, "gateway set c divport <divport>")
{
  uint16_t port;
  const char *argz = pop_argz();
  if (!str2u16(argz, &port)) {
    printf("invalid port\n");
    return;
  }
  get_ifs()->c_divport = port;
}

static void gw_c_profile(void *desc)
{
  GW_ifs *ifs = get_ifs();
  if (ifs->c_divport != 0)
    cmd_load_profile(desc, "gateway set c divport %u", ifs->c_divport);

  if (ifs->c_netstat_interval != 0)
    cmd_load_profile(desc, "gateway set c netstat-interval %u", ifs->c_netstat_interval);


  if (ifs->c_scps_local_udp_port != 0)
    cmd_load_profile(desc, "gateway set c local-udp-port %u", ifs->c_scps_local_udp_port);

  if (ifs->c_scps_remote_udp_port != 0)
    cmd_load_profile(desc, "gateway set c remote-udp-port %u", ifs->c_scps_remote_udp_port);


  if (ifs->c_divert_start_rule != 0)
    cmd_load_profile(desc, "gateway set c div-start-rule %u", ifs->c_divert_start_rule);


  if (ifs->c_divert_insert_rule != 0)
    cmd_load_profile(desc, "gateway set c div-insert-rule %u", ifs->c_divert_insert_rule);

  if (ifs->c_other_proto_xrate_drop != 0)
    cmd_load_profile(desc, "gateway set c xrate-drop %u", ifs->c_other_proto_xrate_drop);

  if (ifs->c_other_proto_non_ip != 0)
    cmd_load_profile(desc, "gateway set c other-nonip %u", ifs->c_other_proto_non_ip);

    
  if (ifs->c_tap_remote_access != 0)
    cmd_load_profile(desc, "gateway set c tap-remote-access %u", ifs->c_tap_remote_access);

  if (ifs->c_other_proto_qlen != 0)
    cmd_load_profile(desc, "gateway set c other-qlen %u", ifs->c_other_proto_qlen);

  if (ifs->c_clust_thresh != 0)
    cmd_load_profile(desc, "gateway set c clust-thresh %u", ifs->c_clust_thresh);
}

BOLT_PROFILE(gw_c,"gw", cmd_gw_c_profile_func, desc)
{

  cmd_load_profile(desc, "\n");
  gw_c_profile(desc);
  cmd_load_profile(desc, "\n");
}
 

        


